<template>
	<view>
		<uni-nav-bar shadow title="" @clickLeft="back" height="88rpx" :border="false" :fixed="true"
			backgroundColor="#FFFFFF" :shadow="false" left-icon="left"></uni-nav-bar>
			
		<form @submit="formSubmit">
			<view class="cu-list menu solid-bottom">
				<view class="cu-item arrow" style="min-height:150rpx;" @tap="chooseImage">
					<view class="content">
						<text style="font-size: 30rpx;">头像</text>
					</view>
					<view class="action">
						<view class="cu-avatar round lg" :style="{backgroundImage: 'url('+ shareInfo.image+')'}" ></view>
					</view>
				</view>
			</view>
			<view class="cu-form-group">
				<view class="title">昵称</view>
				<input placeholder="请输入昵称" name="name" v-model="shareInfo.name"></input>
			</view>
			<view class="cu-form-group">
				<view class="title">手机号</view>
				<input placeholder="请输入手机号" name="mobile" v-model="shareInfo.mobile"></input>
			</view>
			<view class="cu-form-group">
				<view class="title">微信号</view>
				<input placeholder="请输入微信号" name="weixin" v-model="shareInfo.weixin"></input>
			</view>
			<view class="cu-form-group">
				<view class="title">设置个人(企业)信息</view>
				<input placeholder="请输入个人(企业)信息" name="desc" v-model="shareInfo.desc"></input>
			</view>
		
			<view class="padding-bj flex flex-direction">
				<button form-type="submit" class="cu-btn wanl-bg-redorange margin-tb lg">保存</button>
	             <button class="cu-btn line-red lg" @tap="logout()" v-if="from">退出登录</button>
			</view>
		</form>

	</view>
</template>

<script>
	import graceChecker from '@/common/graceChecker';
	import { mapState } from 'vuex';
	export default {
		computed: {
			...mapState(['user','common'])
		},
		data() {
			return {
				gender: true,
				genderdata: 0,
				shareInfo:{
					image:"",
					name:'',
					mobile:'',
					weixin:'',
					desc:"",
					user_id:''
				},
				from:'',
				isRequest:true
			}
		},
		onLoad(option) {
			this.from=option.from&&option.from=='user'?true:false
			this.getShareInfo()
		},
		methods: {
			back() {
				setTimeout(()=>{
					uni.navigateBack({
						delta:1
					})
				},1500)
			},
			chooseImage() {
				uni.chooseImage({
					count: 1, // 默认9
					sizeType: ['compressed'], // 可以指定是原图还是压缩图，默认二者都有
					sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
					success: res => {
						// 设置url的值，显示控件
						// this.user.image = res.tempFilePaths[0];
						this.$api.get({
							url: '/wanlshop/common/uploadData',
							success: updata => {
								this.$api.upload({
									url: updata.uploadurl,
									filePath: res.tempFilePaths[0],
									name: 'file',
									formData: updata.storage == 'local' ? null : updata.multipart,
									success: res => {
										console.log("upload",res.fullurl)
										this.shareInfo.image = res.fullurl;
										this.isRequest=false
									}
								});
							}
						});
					}
				});
			},
			getShareInfo(){
				this.$api.post({
					url: '/topic/shareInfo',
					data:{
						user_id:uni.getStorageSync('userInfo').id,
					}
				}).then(res => {
					this.shareInfo=res
				})
			},
			Gender(e) {
				if(e.detail.value == true){
					this.gender = true;
					this.genderdata = 0;
				}else{
					this.gender = false;
					this.genderdata = 1;
				}
			},
			Avatar(){
				this.$wanlshop.to('/pages/user/portrait/portrait');
			},
			formSubmit: function(e) {
				//定义表单规则
				// ..检查是否注册-没注册跳转注册
				if(this.isRequest){
					this.$wanlshop.msg('头像正在上传，请稍后保存');
					return
				}
					this.$api.post({
						url: '/topic/saveShare', 
						data: this.shareInfo,
						success: res => {
							this.$wanlshop.msg('保存成功');
							uni.setStorageSync("setShareInfo", this.shareInfo);
							setTimeout(()=>{
								uni.navigateBack({
									delta:1
								})
							},1500)
						}
					});
				
			},
			logout() {
				this.$api.get({
					url: '/wanlshop/user/logout',
					data: {
						client_id: uni.getStorageSync("wanlshop:chat_client_id")?uni.getStorageSync("wanlshop:chat_client_id") : null
					},
					success: res => {
						this.$store.dispatch('logout');
						this.$wanlshop.msg('退出成功');
						uni.removeStorageSync('userInfo')
						uni.removeStorageSync('setShareInfo')
						this.$wanlshop.back(1);
					}
				});
			},
		}
	}
</script>

<style>
	.cu-form-group .title {
		min-width: calc(4em + 15px);
	}
</style>
